package com.jic.order.controller.manage;


import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.order.entity.OrderDeliveryRelease;
import com.jic.order.entity.OrderDeliveryReleaseRelation;
import com.jic.order.manage.request.OrderDeliveryReleaseRelationRequest;
import com.jic.order.manage.request.OrderDeliveryReleaseRequest;
import com.jic.order.manage.response.OrderDeliveryReleaseRelationResponse;
import com.jic.order.manage.response.OrderDeliveryReleaseResponse;
import com.jic.order.service.OrderDeliveryReleaseRelationService;
import com.jic.order.service.OrderDeliveryReleaseService;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.after.response.ProductAfterInfoByIdResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(value = "交货单发布管理", tags = {"交货单发布管理"})
@RequestMapping(value = "orderDeliveryRelease")
@RestController
public class OrderDeliveryReleaseController {

    @Autowired
    OrderDeliveryReleaseService orderDeliveryReleaseService;
    @Autowired
    OrderDeliveryReleaseRelationService orderDeliveryReleaseRelationService;
    @Autowired
    ProductListApi productListApi;
    @Autowired
    RedisClientTemplate redisClientTemplate;

    @ApiOperation(value = "获取所有订单拆分信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<OrderDeliveryReleaseResponse>> queryPage(@RequestBody OrderDeliveryReleaseRequest form) {
        log.info("开始调用订单拆分分页，{}", JSON.toJSONString(form));
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        OrderDeliveryRelease orderDeliveryRelease = new OrderDeliveryRelease();
        BeanUtils.copyProperties(form, orderDeliveryRelease);
        PageResult<OrderDeliveryRelease> result = orderDeliveryReleaseService.queryPage(orderDeliveryRelease, page);
        List<OrderDeliveryReleaseResponse> orderDeliveryReleaseResponseList = result.getRows().stream().map(temp -> {
            OrderDeliveryReleaseResponse orderDeliveryReleaseResponse = new OrderDeliveryReleaseResponse();
            BeanUtils.copyProperties(temp, orderDeliveryReleaseResponse);
            return orderDeliveryReleaseResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<OrderDeliveryReleaseResponse> pageResult = new PageResult<>();
        pageResult.setRows(orderDeliveryReleaseResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }

    @ApiOperation(value = "交货单发布新增")
    @ResponseBody
    @PostMapping(value = "/saveOrderDeliveryRelease")
    public RestResult saveOrderDeliveryRelease(@RequestBody OrderDeliveryReleaseRequest orderDeliveryReleaseRequest) {

        RestResult restResult = new RestResult<>();
        try {
            return orderDeliveryReleaseService.saveOrderDeliveryRelease(orderDeliveryReleaseRequest);
        } catch (Exception e) {
            log.error("saveOrderDeliveryRelease========》", e);
        }
        return restResult;

    }

    @ApiOperation(value = "获取所有交货单详情")
    @ResponseBody
    @PostMapping(value = "/queryDetailPage")
    public RestResult<PageResult<OrderDeliveryReleaseRelationResponse>> queryDetailPage(@RequestBody OrderDeliveryReleaseRelationRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());

        OrderDeliveryReleaseRelation relation = new OrderDeliveryReleaseRelation();
        BeanUtils.copyProperties(form, relation);

        return orderDeliveryReleaseRelationService.queryPage(relation, page);
    }

    @ApiOperation(value = "修改转交货信息", notes = "修改转交货信息")
    @PostMapping("/updateDetail")
    public RestResult updateDetail(@RequestBody OrderDeliveryReleaseRelationRequest form) {
        return orderDeliveryReleaseRelationService.update(form);
    }

    @ApiOperation(value = "新增转交货信息", notes = "新增转交货信息")
    @PostMapping("/insertDetail")
    public RestResult insertDetail(@RequestBody OrderDeliveryReleaseRelationRequest form) {
        return orderDeliveryReleaseRelationService.insert(form);
    }


    @ApiOperation(value = "通过订单查询抛单商品", notes = "通过订单查询抛单商品")
    @PostMapping("/queryProductByOrderNo")
    public RestResult<List<OrderDeliveryReleaseResponse>> queryProductByOrderNo(@RequestBody OrderDeliveryReleaseRequest form) {
        OrderDeliveryRelease orderDeliveryRelease = new OrderDeliveryRelease();
        BeanUtils.copyProperties(form, orderDeliveryRelease);
        List<OrderDeliveryRelease> orderDeliveryReleaseList = orderDeliveryReleaseService.query(orderDeliveryRelease);
        List<OrderDeliveryReleaseResponse> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(orderDeliveryReleaseList)) {
            orderDeliveryReleaseList.forEach(item -> {
                OrderDeliveryReleaseResponse orderDeliveryReleaseResponse = new OrderDeliveryReleaseResponse();
                BeanUtils.copyProperties(item, orderDeliveryReleaseResponse);
                RestResult<ProductAfterInfoByIdResponse> responseRestResult = productListApi.queryProductInfoById(item.getProductId());
                if (responseRestResult.getData() != null) {
                    orderDeliveryReleaseResponse.setGoodsName(responseRestResult.getData().getChineseName());
                    orderDeliveryReleaseResponse.setPictureUrl(responseRestResult.getData().getPictureUrl());
                }
                list.add(orderDeliveryReleaseResponse);
            });
            return RestResult.success(list);
        } else {
            return RestResult.success(list);
        }
    }


    @ApiOperation(value = "通过订单查询已购买的抛单商品", notes = "通过订单查询已购买的抛单商品")
    @PostMapping("/queryBuyProductByOrderNo")
    public RestResult<List<OrderDeliveryReleaseRelationResponse>> queryBuyProductByOrderNo(@RequestBody OrderDeliveryReleaseRelationRequest form) {
        OrderDeliveryReleaseRelation orderDeliveryReleaseRelation = new OrderDeliveryReleaseRelation();
        BeanUtils.copyProperties(form, orderDeliveryReleaseRelation);
        List<OrderDeliveryReleaseRelation> orderDeliveryReleaseRelationList = orderDeliveryReleaseRelationService.query(orderDeliveryReleaseRelation);
        List<OrderDeliveryReleaseRelationResponse> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(orderDeliveryReleaseRelationList)) {
            orderDeliveryReleaseRelationList.forEach(item -> {
                OrderDeliveryReleaseRelationResponse orderDeliveryReleaseRelationResponse = new OrderDeliveryReleaseRelationResponse();
                BeanUtils.copyProperties(item, orderDeliveryReleaseRelationResponse);
                RestResult<ProductAfterInfoByIdResponse> responseRestResult = productListApi.queryProductInfoById(item.getProductId());
                if (responseRestResult.getData() != null) {
                    orderDeliveryReleaseRelationResponse.setGoodsName(responseRestResult.getData().getChineseName());
                    orderDeliveryReleaseRelationResponse.setPictureUrl(responseRestResult.getData().getPictureUrl());
                }
                list.add(orderDeliveryReleaseRelationResponse);
            });
            return RestResult.success(list);
        } else {
            return RestResult.success(list);
        }
    }
}
